package com.wsd.jing.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.entity.DictionaryDetail;
import com.wsd.jing.enums.CodeEnum;
import com.wsd.jing.exception.BusinessException;
import com.wsd.jing.mapper.DictionaryDetailMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.IDictionaryDetailService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: DictionaryDetailServiceImpl
 * @description: 数据字典详情服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DictionaryDetailServiceImpl extends ServiceImpl<DictionaryDetailMapper, DictionaryDetail> implements IDictionaryDetailService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertDictionaryDetail(DictionaryDetail dictionaryDetail) {
        QueryWrapper<DictionaryDetail> dictionaryDetailQueryWrapper = new QueryWrapper<>();
        dictionaryDetailQueryWrapper.lambda().eq(DictionaryDetail::getDictionaryId, dictionaryDetail.getDictionaryId()).
                and(wrapper -> wrapper.eq(DictionaryDetail::getName, dictionaryDetail.getName())
                        .or().eq(DictionaryDetail::getContent, dictionaryDetail.getContent())
                );
        List<DictionaryDetail> dictionaryDetailList = this.list(dictionaryDetailQueryWrapper);
        if (CollectionUtil.isNotEmpty(dictionaryDetailList)) {
            throw new BusinessException(CodeEnum.DICTIONARY_DETAIL_NAME_EXIST);
        }
        dictionaryDetail.setCreateTime(LocalDateTime.now());
        return this.save(dictionaryDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDictionaryDetail(DictionaryDetail dictionaryDetail) {
        QueryWrapper<DictionaryDetail> dictionaryDetailQueryWrapper = new QueryWrapper<>();
        dictionaryDetailQueryWrapper.lambda().eq(DictionaryDetail::getDictionaryId, dictionaryDetail.getDictionaryId()).
                and(wrapper -> wrapper.eq(DictionaryDetail::getName, dictionaryDetail.getName())
                        .or().eq(DictionaryDetail::getContent, dictionaryDetail.getContent())
                );
        List<DictionaryDetail> dictionaryDetailList = this.list(dictionaryDetailQueryWrapper);
        if (CollectionUtil.isNotEmpty(dictionaryDetailList) && dictionaryDetailList.size() > 1) {
            throw new BusinessException(CodeEnum.DICTIONARY_DETAIL_NAME_EXIST);
        }
        if (CollectionUtil.isNotEmpty(dictionaryDetailList) && dictionaryDetailList.size() == 1) {
            DictionaryDetail dictionaryDetail1 = this.getById(dictionaryDetail.getId());
            if (!dictionaryDetail.getName().equals(dictionaryDetail1.getName()) && !dictionaryDetail.getContent().equals(dictionaryDetail1.getContent())) {
                throw new BusinessException(CodeEnum.DICTIONARY_DETAIL_NAME_EXIST);
            }
        }
        dictionaryDetail.setUpdateTime(LocalDateTime.now());
        return this.updateById(dictionaryDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDictionaryDetail(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDictionaryDetails(String ids) {
        // TODO 删除时需判断在其他业务表中是否使用该记录
        List<String> idList = StrUtil.splitTrim(ids, ",");
        return this.removeByIds(idList);
    }

    @Override
    public DictionaryDetail getDictionaryDetail(Integer id) {
        // TODO 删除时需判断在其他业务表中是否使用该记录
        return this.getById(id);
    }

    @Override
    public List<DictionaryDetail> listDictionaryDetails(CommonRequest commonRequest, DictionaryDetail dictionaryDetail) {
        QueryWrapper<DictionaryDetail> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageDictionaryDetails(CommonRequest commonRequest, DictionaryDetail dictionaryDetail) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<DictionaryDetail> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<DictionaryDetail> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (StrUtil.isNotBlank(dictionaryDetail.getName())) {
            queryWrapper.lambda().like(DictionaryDetail::getName, dictionaryDetail.getName());
        }
        queryWrapper.lambda().eq(DictionaryDetail::getDictionaryId, dictionaryDetail.getDictionaryId());
        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<DictionaryDetail> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
